package com.mall.order.controller;

import com.mall.api.order.dto.OrderDTO;
import com.mall.api.product.ProductFeignClient;
import com.mall.api.product.dto.ProductDTO;
import com.mall.common.result.Result;
import com.mall.order.service.OrderService;
import io.seata.spring.annotation.GlobalTransactional;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/transaction-test")
@RequiredArgsConstructor
@Tag(name = "事务测试接口", description = "用于测试分布式事务")
public class TransactionTestController {

    private final OrderService orderService;
    private final ProductFeignClient productFeignClient;

    @GetMapping("/create-order-success")
    @Operation(summary = "创建订单-成功", description = "测试分布式事务正常提交")
    @GlobalTransactional(name = "tx_test_success", rollbackFor = Exception.class)
    public Result<String> createOrderSuccess() {
        log.info("开始创建订单-成功测试");
        
        // 获取商品
        Result<ProductDTO> productResult = productFeignClient.getProductById(1L);
        if (productResult.getCode() != 200 || productResult.getData() == null) {
            return Result.error(500, "商品不存在");
        }
        
        ProductDTO product = productResult.getData();
        
        // 构建订单
        OrderDTO orderDTO = new OrderDTO();
        orderDTO.setUserId(1L);
        orderDTO.setTotalAmount(product.getPrice());
        orderDTO.setStatus(0);
        orderDTO.setAddress("测试地址");
        orderDTO.setPhone("13800138000");
        orderDTO.setReceiver("测试用户");
        
        List<OrderDTO.OrderItemDTO> items = new ArrayList<>();
        OrderDTO.OrderItemDTO item = new OrderDTO.OrderItemDTO();
        item.setProductId(product.getId());
        item.setQuantity(1);
        item.setPrice(product.getPrice());
        items.add(item);
        
        orderDTO.setItems(items);
        
        // 创建订单
        orderService.createOrder(orderDTO);
        
        log.info("订单创建成功");
        return Result.success("订单创建成功");
    }
    
    @GetMapping("/create-order-fail")
    @Operation(summary = "创建订单-失败", description = "测试分布式事务回滚")
    @GlobalTransactional(name = "tx_test_fail", rollbackFor = Exception.class)
    public Result<String> createOrderFail() {
        log.info("开始创建订单-失败测试");
        
        // 获取商品
        Result<ProductDTO> productResult = productFeignClient.getProductById(1L);
        if (productResult.getCode() != 200 || productResult.getData() == null) {
            return Result.error(500, "商品不存在");
        }
        
        ProductDTO product = productResult.getData();
        
        // 构建订单
        OrderDTO orderDTO = new OrderDTO();
        orderDTO.setUserId(1L);
        orderDTO.setTotalAmount(product.getPrice());
        orderDTO.setStatus(0);
        orderDTO.setAddress("测试地址");
        orderDTO.setPhone("13800138000");
        orderDTO.setReceiver("测试用户");
        
        List<OrderDTO.OrderItemDTO> items = new ArrayList<>();
        OrderDTO.OrderItemDTO item = new OrderDTO.OrderItemDTO();
        item.setProductId(product.getId());
        item.setQuantity(1);
        item.setPrice(product.getPrice());
        items.add(item);
        
        orderDTO.setItems(items);
        
        // 创建订单
        orderService.createOrder(orderDTO);
        
        // 模拟异常
        log.info("模拟异常，触发事务回滚");
        throw new RuntimeException("模拟异常，测试分布式事务回滚");
    }
} 